package cn.thornbird.orgsync.model.dingtalk;

import cn.thornbird.orgsync.model.AppUserData;
import cn.thornbird.orgsync.util.JsonUtil;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class UserInfo extends UserList.User {

    public static final String CREATE_FIELDS = "name,mobile,dept_id_list,dept_order_list,dept_title_list,email," +
            "extension,hide_mobile,hired_date,job_number,login_email,manager_userid,org_email,org_email_type,remark," +
            "senior_mode,telephone,title,userid,work_place";

    public static final String UPDATE_FIELDS = "userid,dept_id_list,dept_order_list,email,extension," +
            "force_update_fields,hide_mobile,hired_date,job_number,language,manager_userid,name,org_email,remark," +
            "senior_mode,telephone,title,work_place";

    /**
     * 员工企业邮箱类型: profession-标准版, base-基础版
     */
    @JsonProperty("org_email_type")
    private String orgEmailType;

    /**
     * 员工在对应的部门中的职位
     */
    @JsonProperty("dept_title_list")
    private List<DepartmentTitle> deptTitleList;

    /**
     * 是否开启高管模式
     */
    @JsonProperty("senior_mode")
    private Boolean seniorMode;

    /**
     * 登录邮箱
     */
    @JsonProperty("login_email")
    private String loginEmail;

    /**
     * 强制更新的字段, 支持清空指定的字段, 多个字段之间使用逗号分隔
     */
    @JsonProperty("force_update_fields")
    private String forceUpdateFields;

    public Map<String, Object> toCreateParams() {
        return toParams(CREATE_FIELDS);
    }

    public Map<String, Object> toUpdateParams() {
        return toParams(UPDATE_FIELDS);
    }

    public Map<String, Object> toParams(String fields) {
        Map<String, Object> params = JsonUtil.toMap(this, StringUtils.isEmpty(fields) ? null : fields.split(","));
        if (params.containsKey("dept_id_list") && this.getDeptIdList() != null) {
            params.put("dept_id_list", this.getDeptIdList().stream().map(String::valueOf).collect(
                    Collectors.joining(",")));
        }
        return params;
    }

    public UserInfo setId(String id) {
        setUserId(id);
        return this;
    }

    public UserInfo setDepartments(Map<String, AppUserData.DepartmentInfo> departments, String mainDepartmentId) {
        if (departments != null && !departments.isEmpty()) {
            setDeptIdList(new ArrayList<>());
            setDeptOrderList(new ArrayList<>());
            setLeaderInDept(new ArrayList<>());
            long currentOrder = 0;
            for (Map.Entry<String, AppUserData.DepartmentInfo> entry: departments.entrySet()) {
                Long departmentId = null;
                try {
                    departmentId = Long.parseLong(entry.getKey());
                } catch (NumberFormatException e) {
                }
                if (departmentId != null) {
                    currentOrder++;
                    getDeptIdList().add(departmentId.toString());
                    UserList.DepartmentOrder order = new UserList.DepartmentOrder();
                    order.setDeptId(departmentId);
                    order.setOrder((entry.getValue().getOrder() == null || entry.getValue().getOrder() <= 0) ?
                            currentOrder : entry.getValue().getOrder());
                    getDeptOrderList().add(order);
                    UserList.DepartmentLeader leader = new UserList.DepartmentLeader();
                    leader.setDeptId(departmentId);
                    leader.setLeader(entry.getValue().getLeader());
                    getLeaderInDept().add(leader);
                }
            }
        }
        return this;
    }

    public UserInfo setLeaderId(String leaderId) {
        if (!StringUtils.isEmpty(leaderId)) {
            setManagerUserId(leaderId);
        } else {
            setManagerUserId(null);
        }
        return this;
    }


    @Data
    public static class DepartmentTitle {

        @JsonProperty("dept_id")
        private Long deptId;

        @JsonProperty("title")
        private String title;

    }

}
